import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { ACLService } from '@delon/acl';
import { SelectDateComponent } from '../select-date/select-date.component';

@Component({
  selector: 'app-device-log',
  templateUrl: './device-log.component.html',
  styles: []
})
export class DeviceLogComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any;
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  config: SPage = {
    columns: [
      {
        index:"",
        title:"选择",
        type:ColumnType.radio
      },
      {
        index: 'deviceCode',
        title: '设备SN码',
        width:130,
        filter: {
          placeholder: '请输入设备SN码',
          type: 'string'
        }
      },
      {
        index: 'deviceDescribe',
        title: '设备描述',
        width:100,
        filter: {
          placeholder: '请输入设备描述',
          type: 'string'
        }
      },
      {
        index: 'deviceFac',
        title: '设备厂商',
        width:100,
        filter: {
          placeholder: '请输入厂商',
          type: 'string'
        }
      },
      {
        index:'deviceType',
        title:'设备分类',
        width:100,
        type: ColumnType.tag,
        tag:{
          1:{text: '门禁摄像机'},
          2:{text: '门禁刷卡'},
          3:{text: 'IPC'},
          4:{text: 'NVR'},
          5:{text: '微型门禁产品'},
          6:{text: '安卓门禁产品'},
          7:{text: '门铃'},
          8:{text: '4G开门控制器'},
          12:{text: 'linux人脸机'}
          
        },
        filter: {
          placeholder: '请选择设备分类',
          type: 'select',
          items: [
            {label: '门禁摄像机', value: 1},
            {label: '门禁刷卡', value: 2},
            {label: 'IPC', value: 3},
            {label: 'NVR', value: 4},
            {label: '微型门禁产品', value: 5},
            {label: '安卓门禁产品', value: 6},
            {label: '门铃', value: 7},
            {label: '4G开门控制器',value: 8},
            {label: 'linux人脸机',value: 12}
          ]
        }
      },
      {
        index: 'deviceCategory',
        title: '设备类型',
        width:100,
        type: ColumnType.tag,
        tag: {
          1:{text: '大门口机'},
          2:{text: '管理机'},
          3:{text: '单元机'},
          4:{text:'室内机'}
        },
      },
      {
        index: 'deviceLocation',
        title: '安装地址',
        width:500,
        filter: {
          placeholder: '请输入安装地址',
          type: 'string'
        },
        //className:'w200'
      },
      {
        index: 'deviceLocationStatus',
        title: '绑定状态',
        width:100,
        type: ColumnType.tag,
        tag: {
          0:{text: '未绑定',color: 'red'},
          1:{text: '已绑定',color: 'green'}
        },
        filter: {
          placeholder: '请选择绑定状态',
          type: 'select',
          items: [
            {label: '未绑定', value: 0},
            {label: '已绑定', value: 1},
          ]
        }
      },
      {
        index: 'deviceLastStatus',
        title: '设备状态',
        width:100,
        type: ColumnType.tag,
        tag:{
          0:{text: '离线',color: 'red'},
          1:{text: '在线',color: 'green'}
        },
        filter: {
          placeholder: '请选择设备状态',
          type: 'select',
          items: [
            {label: '离线', value: 0},
            {label: '在线', value: 1},
          ]
        }
      },
      {
        index: 'deviceIp',
        title: '门禁IP',
        width:120,
        filter:{
          placeholder:'请输入设备IP地址',
          type:'string'
        }
      },
      {
        index: 'status',
        title: '状态',
        width:80,
        type: ColumnType.tag,
        tag:{
          true:{text:'启用',color:'green'},
          false:{text:'停用',color:'red'}
        }
      },
    ],
    showCheckBox: false,
    reqUrl:'/core/host/page',
    defaultParam:{status:true}
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService) { }
  
  ngOnInit() {

  }

  // 编辑功能开始
  editDic( _param?: object): void {
    const modal = this.modal.create({
      nzTitle: "设备日志",
      nzContent: "",
      nzWidth: 1300,
      nzComponentParams: {
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
 
  _console(e: any): void {
    this.selectData = e;
  }
  dataProcess(data: STData[]){
    console.log(data);
    for(let item of data){
      item.deviceFac="兴天下";
    }
    return data;
  }
  //获取日志
  getLog(){
    if(this.selectData == undefined){
      this.message.error("请选择设备!");
      return;
    }    
    console.log(this.selectData);
    const _param=this.selectData
    const modal = this.modal.create({
      nzTitle: '选择日期',
      nzContent: SelectDateComponent,
      nzWidth: 800,
      nzComponentParams: {
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
  }

  //获取数据库
  getDataBase(){
    if(this.selectData == undefined){
      this.message.error("请选择设备!");
      return;
    }    
    const filePath1=`https://www.xtxspmj.com/image-storage/doc/${this.selectData.deviceCode}/data.mdb`
    const filePath2=`https://www.xtxspmj.com/image-storage/doc/${this.selectData.deviceCode}/lock.mdb`
    this.sendOrder();
    this.downLoadFile(filePath1)
    this.downLoadFile(filePath2)
  }
  downLoadFile(filePath){
    var elemIF = document.createElement("iframe");
    elemIF.src =filePath;
    elemIF.style.display = "none";
    document.body.appendChild(elemIF);
  }

  //下发获取数据库的命令
  sendOrder(){
    const body={
      sn:this.selectData.deviceCode,
    }
    this.http.request<any>('post', '/core/host/downloadFaceLog',{body}).subscribe(res => {
      console.log(res)
      if (res.status === '200') {
        this.message.success('已下发设备上传数据库命令');
      } else {
        this.message.error(res.message)
      }
    })
  }

   /**
   * 判断服务的文件是否存在
   * @param filepath 文件地址
   * @param filename
   * @returns {Boolean}  
   */
    isExistFile(filepath){
      var xmlhttp;
      if ((window as any).XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
      }
      xmlhttp.open("GET",filepath,false);
      xmlhttp.send();
      if(xmlhttp.readyState === 4){
      if(xmlhttp.status === 200) return true; //url存在
      else if(xmlhttp.status === 404) return false; //url不存在
      else return false;//其他状态
  }
}
}
